import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import { Loading, Message } from 'element-ui';

import axios from 'axios'
axios.defaults.baseURL = 'http://localhost:5000'
Vue.prototype.$http = axios

let loadingInstance = null
axios.interceptors.request.use(config => {
  loadingInstance = Loading.service({ fullscreen: true })
  if(localStorage.getItem('token')){
    config.headers.Authorization = localStorage.getItem('token')
  }
  return config
}, err => {
  return Promise.reject(err)
})

axios.interceptors.response.use(response => {
  loadingInstance.close()
  return response
}, err => {
  loadingInstance.close()
  Message.error(err.response.data)
  const {status} = err.response
  if(status == 401){
    Message.error('token失效,请重新登录！')
    localStorage.removeItem('token')
    router.push('/login')
  }
  return Promise.reject(err)
})


Vue.config.productionTip = false

Vue.use(ElementUI);

Vue.filter('roleFormat', role => {
  if(role === 0) return '员工'
  return '管理员'
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
